package com.lxmes.sales.service.impl;

import java.util.Date;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.sales.domain.*;
import com.lxmes.sales.mapper.NcAssemblyFormalSchedulingPlanMapper;
import com.lxmes.sales.mapper.NcProductPreSchedulingPlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.sales.mapper.NcAssemblyPreSchedulingPlanMapper;
import com.lxmes.sales.service.INcAssemblyPreSchedulingPlanService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;

/**
 * 部装预排产计划Service业务层处理
 *
 * @author lx
 * @date 2023-04-11
 */
@Service
public class NcAssemblyPreSchedulingPlanServiceImpl implements INcAssemblyPreSchedulingPlanService
{
    @Autowired
    private NcAssemblyPreSchedulingPlanMapper ncAssemblyPreSchedulingPlanMapper;

    @Autowired
    private NcAssemblyFormalSchedulingPlanMapper ncAssemblyFormalSchedulingPlanMapper;
    @Autowired
    private NcProductPreSchedulingPlanMapper ncProductPreSchedulingPlanMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询部装预排产计划
     *
     * @param id 部装预排产计划主键
     * @return 部装预排产计划
     */
    @Override
    public NcAssemblyPreSchedulingPlan selectNcAssemblyPreSchedulingPlanById(Long id)
    {
        return ncAssemblyPreSchedulingPlanMapper.selectNcAssemblyPreSchedulingPlanById(id);
    }

    /**
     * 查询部装预排产计划列表
     *
     * @param ncAssemblyPreSchedulingPlan 部装预排产计划
     * @return 部装预排产计划
     */
    @Override
    public List<NcAssemblyPreSchedulingPlan> selectNcAssemblyPreSchedulingPlanList(NcAssemblyPreSchedulingPlan ncAssemblyPreSchedulingPlan)
    {
        return ncAssemblyPreSchedulingPlanMapper.selectNcAssemblyPreSchedulingPlanList(ncAssemblyPreSchedulingPlan);
    }

    /**
     * 新增部装预排产计划
     *
     * @param ncAssemblyPreSchedulingPlan 部装预排产计划
     * @return 结果
     */
    @Override
    public int insertNcAssemblyPreSchedulingPlan(NcAssemblyPreSchedulingPlan ncAssemblyPreSchedulingPlan)
    {
        return ncAssemblyPreSchedulingPlanMapper.insertNcAssemblyPreSchedulingPlan(ncAssemblyPreSchedulingPlan);
    }

    /**
     * 修改部装预排产计划
     *
     * @param ncAssemblyPreSchedulingPlan 部装预排产计划
     * @return 结果
     */
    @Override
    public int updateNcAssemblyPreSchedulingPlan(NcAssemblyPreSchedulingPlan ncAssemblyPreSchedulingPlan)
    {
        return ncAssemblyPreSchedulingPlanMapper.updateNcAssemblyPreSchedulingPlan(ncAssemblyPreSchedulingPlan);
    }

    /**
     * 批量删除部装预排产计划
     *
     * @param ids 需要删除的部装预排产计划主键
     * @return 结果
     */
    @Override
    public int deleteNcAssemblyPreSchedulingPlanByIds(Long[] ids)
    {
        return ncAssemblyPreSchedulingPlanMapper.deleteNcAssemblyPreSchedulingPlanByIds(ids);
    }

    /**
     * 删除部装预排产计划信息
     *
     * @param id 部装预排产计划主键
     * @return 结果
     */
    @Override
    public int deleteNcAssemblyPreSchedulingPlanById(Long id)
    {
        return ncAssemblyPreSchedulingPlanMapper.deleteNcAssemblyPreSchedulingPlanById(id);
    }
    @Override
    public String imporIteminfo(List<NcAssemblyPreSchedulingPlan> ncAssemblyPreSchedulingPlanList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncAssemblyPreSchedulingPlanList) || ncAssemblyPreSchedulingPlanList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcAssemblyPreSchedulingPlan ncAssemblyPreSchedulingPlan : ncAssemblyPreSchedulingPlanList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncAssemblyPreSchedulingPlan);
                ncAssemblyPreSchedulingPlan.setCreateBy(operName);
                ncAssemblyPreSchedulingPlan.setUpdateBy(operName);
                this.insertNcAssemblyPreSchedulingPlan(ncAssemblyPreSchedulingPlan);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    @Transactional
    public int formalProduction(Long[] ids) {
        for(long id : ids){
            NcAssemblyPreSchedulingPlan ncAssemblyPreSchedulingPlan = ncAssemblyPreSchedulingPlanMapper.selectNcAssemblyPreSchedulingPlanById(id);
            NcAssemblyFormalSchedulingPlan ncAssemblyFormalSchedulingPlan = new NcAssemblyFormalSchedulingPlan();
            BeanUtils.copyProperties(ncAssemblyPreSchedulingPlan,ncAssemblyFormalSchedulingPlan);
            ncAssemblyFormalSchedulingPlan.setCreateBy(SecurityUtils.getUsername());
            ncAssemblyFormalSchedulingPlan.setCreateTime(new Date());
            ncAssemblyFormalSchedulingPlanMapper.insertNcAssemblyFormalSchedulingPlan(ncAssemblyFormalSchedulingPlan);
            ncAssemblyPreSchedulingPlanMapper.deleteNcAssemblyPreSchedulingPlanById(id);


            //资源锁账变为计划锁账
            CcStockInfoVo ccStockinfoVo = new CcStockInfoVo();
            ccStockinfoVo.setItemCode(ncAssemblyPreSchedulingPlan.getGwItemCode());
            CcStockInfoVo ccStockInfoVo = ncProductPreSchedulingPlanMapper.selectStockInfo(ccStockinfoVo);
            ccStockinfoVo.setPlanLocking(ccStockInfoVo.getResourceLocking());
            ncProductPreSchedulingPlanMapper.updateStockPlanLockNum(ccStockinfoVo);
        }
        return 1;
    }
}
